﻿namespace USElections.Map.Classes
{
    using System.Collections.Generic;
    using USElections.Utils;

    /// <summary>
    /// StateDataByCode class.
    /// </summary>
    public class StateDataByCode : List<ItemInGroup<Model.JSON.State>>
    {
        #region Public methods
        /// <summary>
        /// Initializes a new instance of the StateDataByCode class.
        /// </summary>
        /// <param name="states">The states class to populate the list.</param>
        public StateDataByCode(Model.JSON.States states)
        {
            Dictionary<string, ItemInGroup<Model.JSON.State>> groups = new Dictionary<string, ItemInGroup<Model.JSON.State>>();

            foreach (var item in Model.JSON.States.StatesWithFullNames)
            {
                ItemInGroup<Model.JSON.State> group = new ItemInGroup<Model.JSON.State>(item.Key);
                this.Add(group);
                groups[item.Key] = group;
            }

            if (states != null)
            {
                foreach (Model.JSON.State state in states)
                {
                    if (groups.ContainsKey(state.Name))
                    {
                        groups[state.Name].Add(state);
                    }
                    else
                    {
                        // XXX: debug
                    }
                }
            }
        }
        #endregion
    }
}
